package mpcs.utils.xml
{
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;

	/**
	 * 读写xml文件的工具类
	 * @author zhangzuoqiang
	 */	
	public class XMLUtils {
		
		private static var file:File;
		
		/**
		 * 加载XML文件
		 * @param filepath 路径
		 * @return xml
		 */
		public static function loadXMLFile( filepath:String ):XML {
			if(filepath == null)
				return null;
			trace("load XML file success ... ");
			// load XML file
			file = new File(File.applicationDirectory.resolvePath(filepath).nativePath);
			var fs:FileStream = new FileStream();
			fs.open(file, FileMode.READ);
			var xmlString:String = fs.readUTFBytes(fs.bytesAvailable);
			fs.close();
			return new XML(xmlString);
		}
		
		/**
		 * 保存xml对象到文件 
		 * @param xml
		 * @param filepath
		 */		
		public static function saveXMLtoFile( xml:XML, filepath:String ):void {
			var fs:FileStream = new FileStream();
			fs.open(file, FileMode.WRITE);
			fs.writeUTFBytes(xml.toXMLString());
			fs.close(); 
		}
		
		
	}
}